﻿using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace WholeSlideImageLibrary
{
    public abstract class DirectorySourceInfo
    {
        public virtual string? DirectoryName { get; }

        public abstract IEnumerable<string> ListFiles();

        public abstract IEnumerable<string> ListDirectory();

        public virtual bool CheckFileExists(string fileName) => ListFiles().Any(f => string.CompareOrdinal(f, fileName) == 0);

        public virtual bool CheckDirectoryExists(string directoryName) => ListFiles().Any(f => string.CompareOrdinal(f, directoryName) == 0);

        public abstract ValueTask<FileSourceInfo?> GetFileAsync(string fileName, CancellationToken cancellationToken = default);

        public virtual async ValueTask<FileSourceReader?> OpenFileAsync(string fileName, CancellationToken cancellationToken = default)
        {
            FileSourceInfo? file = await GetFileAsync(fileName, cancellationToken).ConfigureAwait(false);
            if (file is null)
            {
                return null;
            }
            return await file.OpenAsync(cancellationToken).ConfigureAwait(false);
        }

        public abstract ValueTask<DirectorySourceInfo?> GetDirectoryAsync(string directoryName, CancellationToken cancellationToken = default);

    }
}
